﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using Lavka.Models;

namespace Lavka.Core
{
    public static class CategoriesHelper
    {
        public static List<Caterories> GetCategories(bool includeRoot, LavkaDbEntities context)
        {
            if (includeRoot)
            {
                return context.Caterories.ToList();
            }
            var rootCategory = context.Caterories.Where(x => x.ParentId == null);
            return context.Caterories.Except(rootCategory).ToList();
        }
        public static Caterories GetCategoryById(int? categoryId, LavkaDbEntities context)
        {
            if (categoryId == null)
            {
                return context.Caterories.Where(x => x.ParentId == null).FirstOrDefault();
            }
            return context.Caterories.Where(c => c.CategoryId == categoryId).FirstOrDefault();
        }
        public static List<Articles> GetCategoryArticles(int categoryId, LavkaDbEntities context)
        {
            var category = context.Caterories.Where(c => c.CategoryId == categoryId).FirstOrDefault();
            if (category != null) return category.Articles.ToList();
            return null;
        }
    }
}